package com.victor.zjbj_android.yichuli;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.victor.zjbj_android.R;
import com.victor.zjbj_android.yichuli.fragment.YiChuLiFragment;
import com.victor.zjbj_android.yinhuan.MyYinHuanPagerAdapter;
import com.victor.zjbj_android.yinhuan.adapter.MyExAdapter;
import com.victor.zjbj_android.bean.GroupBean;
import com.victor.zjbj_android.yinhuan.bean.ItemBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class YichuliActivity extends AppCompatActivity implements OnDateSetListener {
    private TabLayout mTablayout;
    private ViewPager mViewPager;
    private List<Fragment> fragments;

    private YiChuLiFragment myFragment1;
    private YiChuLiFragment myFragment2;

    private void initFragments() {
        myFragment1 = YiChuLiFragment.newInstance(0);
        myFragment2 = YiChuLiFragment.newInstance(1);
        fragments = new ArrayList<>();
        fragments.add(myFragment1);
        fragments.add(myFragment2);
    }
    ArrayList<GroupBean> groupList ; //存储部门数据
    ArrayList<List> childList ; //存储装有人员信息的List
    private TextView tv_type;
    private TextView tv_time;
    ExpandableListView expandableListView;
    private RelativeLayout rl_type;
    SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
    TimePickerDialog mDialogYearMonthDay;
    private RelativeLayout rl_starttime;
    private RelativeLayout rl_endtime;
    private TextView tv_starttime;
    private TextView tv_endtime;
    private RelativeLayout rl_time;
    private int timeType;
    private int typeState;
    private int timeState;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yichuli);
        typeState=0;
        timeState=0;
        initFragments();
        initView();
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //填充数据
        initData();


        expandableListView = (ExpandableListView) findViewById(R.id.view_exlist);
        //new一个适配器，就是之前我们创建的适配器，并且填入参数
        MyExAdapter adapter = new MyExAdapter(this,//Activity自带Context属性，所以传this进去就行了
                R.layout.item_group_exlist,//我们之前创建的显示部门名称的布局
                R.layout.item_exlist, //我们之前创建的显示部门人员信息的布局
                groupList, //刚刚创建的装有部门数据的List对象
                childList); //刚刚创建的装有人员信息的List的List对象

        //设置适配器，传入刚刚new的adapter适配的对象
        expandableListView.setAdapter(adapter);
    }
    private void initView() {
        mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(YichuliActivity.this)
                .setThemeColor(getResources().getColor(R.color.colorPrimary))
                .setToolBarTextColor(getResources().getColor(R.color.colorPrimary))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.colorPrimary))
                .build();

        rl_time=findViewById(R.id.rl_time);
        rl_starttime=findViewById(R.id.rl_starttime);
        rl_endtime=findViewById(R.id.rl_endtime);
        tv_starttime=findViewById(R.id.tv_starttime);
        tv_endtime=findViewById(R.id.tv_endtimetime);
        tv_type=findViewById(R.id.tv_type);
        tv_time=findViewById(R.id.tv_time);
        rl_type=findViewById(R.id.rl_type);
        mViewPager=findViewById(R.id.viewPager);
        mTablayout=findViewById(R.id.tb_wenjian);
        MyYinHuanPagerAdapter myWenjianPagerAdapter=new MyYinHuanPagerAdapter(getSupportFragmentManager(),fragments,new String[]{"已处理隐患","已接收任务"});
        mViewPager.setAdapter(myWenjianPagerAdapter);
        mTablayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
//                        myFragment1.refresh(0);
                        break;
                    case 1:
//                        myFragment2.refresh(1);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tv_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (typeState==0){
                    rl_type.setVisibility(View.VISIBLE);
                    rl_time.setVisibility(View.GONE);
                    typeState=1;
                }else {
                    rl_type.setVisibility(View.GONE);
                    rl_time.setVisibility(View.GONE);
                    typeState=0;
                }



            }
        });
        tv_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(timeState==0){
                    rl_type.setVisibility(View.GONE);
                    rl_time.setVisibility(View.VISIBLE);
                    timeState=1;
                }else {
                    rl_type.setVisibility(View.GONE);
                    rl_time.setVisibility(View.GONE);
                    timeState=0;
                }


            }
        });
        rl_starttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timeType=0;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
            }
        });
        rl_endtime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timeType=1;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
            }
        });
    }
    public void initData() {
        //初始化变量
        groupList = new ArrayList();
        childList = new ArrayList();

        //添加部门信息
        groupList.add(new GroupBean("文明施工"));
        groupList.add(new GroupBean("安全培训"));
        groupList.add(new GroupBean("权限目录"));
        groupList.add(new GroupBean("安全管理"));
        //添加销售部门的人员信息
        ArrayList xs = new ArrayList();
        xs.add(new ItemBean("文明施工1"));
        xs.add(new ItemBean("文明施工1"));
        xs.add(new ItemBean("文明施工1"));
        childList.add(xs);

        //添加主管部门的人员信息
        ArrayList zg = new ArrayList();
        zg.add(new ItemBean("安全培训1"));
        zg.add(new ItemBean("安全培训1"));
        zg.add(new ItemBean("安全培训1"));
        childList.add(zg);

        //添加售后部门的人员信息
        ArrayList sh = new ArrayList();
        sh.add(new ItemBean("权限目录1"));
        sh.add(new ItemBean("权限目录1"));
        sh.add(new ItemBean("权限目录1"));
        childList.add(sh);

        //添加售后部门的人员信息
        ArrayList sh1 = new ArrayList();
        sh1.add(new ItemBean("安全管理1"));
        sh1.add(new ItemBean("安全管理1"));
        sh1.add(new ItemBean("安全管理1"));
        childList.add(sh1);
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        String text = getDateToString(millseconds);
        if(timeType==0){
            tv_starttime.setText(text);
        }else {
            tv_endtime.setText(text);
        }
        Log.e("11",text);
    }
    public String getDateToString(long time) {
        Date d = new Date(time);
        return sf.format(d);
    }
    /**
     * 强制隐藏键盘
     */
    public void forceHideIM() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager == null) {
            return;
        }
        View decorView = getWindow().getDecorView();
        inputMethodManager.hideSoftInputFromWindow(decorView.getWindowToken(), 0);
    }
}
